package com.aos.erp.gov.ex.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.utils.JsonUtils;
import com.aos.erp.common.utils.StringUtils;
import com.aos.erp.gov.ex.enums.RegisterTaskStatus;
import com.aos.erp.gov.ex.enums.RegisterTaskType;
import com.aos.erp.gov.ex.mapper.GovExRegisterTaskMapper;
import com.aos.erp.gov.ex.model.GovExRegisterTask;
import com.aos.erp.gov.ex.model.dto.CheckResultFailDTO;
import com.aos.erp.gov.ex.model.dto.QueryRegisterTaskDTO;
import com.aos.erp.gov.ex.service.GovExRegisterTaskService;
import com.aos.erp.gov.ex.task.mq.QueueConstants;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;


@Slf4j
@Service
public class GovExRegisterTaskServiceImpl extends ServiceImpl<GovExRegisterTaskMapper, GovExRegisterTask> implements GovExRegisterTaskService {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    @Override
    public GovExRegisterTask getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void addBean(GovExRegisterTask bean) {
        bean.setId(bean.getStudentId());
        GovExRegisterTask temp = this.getById(bean.getId());
        if (temp == null) {
            if (bean.getSubmitTime() == null) {
                bean.setSubmitTime(Calendar.getInstance().getTime());
            }
            baseMapper.insert(bean);
        } else {
            log.info("数据已经存在" + bean.getId() + "-" + bean.getStudentIdnum());
        }
    }

    @Override
    public PageBean<GovExRegisterTask> queryByPage(QueryRegisterTaskDTO dto) {

        IPageBean<GovExRegisterTask> pageBean = baseMapper.createBizPage(dto.getPageIndex(), dto.getPageSize());
        QueryWrapper<GovExRegisterTask> queryWrapper = baseMapper.createQuery();
        if (StringUtils.isNotEmpty(dto.getProvinceCode())) {
            queryWrapper.eq("province_code", dto.getProvinceCode());
        }

        if (StringUtils.isNotEmpty(dto.getCityCode())) {
            queryWrapper.eq("city_code", dto.getCityCode());
        }

        if (dto.getSchoolId() != 0) {
            queryWrapper.eq("school_id", dto.getSchoolId());
        }

        if (StringUtils.isNotEmpty(dto.getText())) {
            queryWrapper.and(wrapper -> wrapper.like("student_name", dto.getText()).or().like("studentIdnum", dto.getText()));
        }

        //创建时间
        if (dto.getType() == 0) {
            if (dto.getStartTime() != null) {
                queryWrapper.ge("create_time", dto.getStartTime());
            } else if (dto.getEndTime() != null) {
                queryWrapper.le("create_time", dto.getEndTime());
            }
        } else {
            if (dto.getStartTime() != null) {
                queryWrapper.ge("submit_time", dto.getStartTime());
            } else if (dto.getEndTime() != null) {
                queryWrapper.le("submit_time", dto.getEndTime());
            }
        }

        queryWrapper.eq("status", RegisterTaskStatus.none);


        return (PageBean<GovExRegisterTask>) baseMapper.selectPage(pageBean, queryWrapper);
    }

    @Override
    public int saveSubmitted(List<String> idnums, RegisterTaskType type) {
        return baseMapper.saveSubmitted(idnums, type, Calendar.getInstance().getTime());
    }

    @Override
    public int saveSubmitted(String idnum, RegisterTaskType type, boolean noticeSchool) {
        int re = baseMapper.saveOneSubmitted(idnum, type, Calendar.getInstance().getTime());
        if (re > 0 && noticeSchool) {
            if (re > 0 && noticeSchool) {
                rabbitTemplate.convertAndSend(QueueConstants.toSchoolStudentSubmitted, new String[]{idnum});
                log.info("发送" + QueueConstants.toSchoolStudentSubmitted + "消息：" + JsonUtils.toJSONString(new String[]{idnum}));
            }
        }
        return re;
    }

    @Override
    public int saveConfirmSuccess(List<String> idnums, RegisterTaskType type) {
        return baseMapper.saveSuccess(idnums, type, Calendar.getInstance().getTime());
    }

    @Override
    public int saveConfirmSuccess(String idnum, RegisterTaskType type, boolean noticeSchool) {
        int re = baseMapper.saveOneSuccess(idnum, type, Calendar.getInstance().getTime());
        if (re > 0 && noticeSchool) {
            rabbitTemplate.convertAndSend(QueueConstants.toSchoolStudentSuccess, new String[]{idnum});
            log.info("发送" + QueueConstants.toSchoolStudentSuccess + "消息：" + JsonUtils.toJSONString(new String[]{idnum}));
        }
        return re;
    }

    @Override
    public int saveConfirmFailed(List<String> idnums, RegisterTaskType type) {
        return baseMapper.saveFailed(idnums, type, Calendar.getInstance().getTime());
    }

    @Override
    public int saveConfirmFailed(CheckResultFailDTO failDTO, RegisterTaskType type, boolean noticeSchool) {
        int re = baseMapper.saveOneFailed(failDTO.getIdNo(), type, Calendar.getInstance().getTime());
        if (re > 0 && noticeSchool) {
            if (re > 0 && noticeSchool) {
                rabbitTemplate.convertAndSend(QueueConstants.toSchoolStudentFailed, failDTO);
                log.info("发送" + QueueConstants.toSchoolStudentFailed + "消息：" + JsonUtils.toJSONString(failDTO));
            }
        }
        return re;
    }
}